
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>在 JavaScript 中交换值的 10 种方法 · 记录技术文档内容</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="zhengyuliang">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-sectionx/sectionx.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="常用的前端JavaScript方法封装.html" />
    
    
    <link rel="prev" href="前端异常的捕获与处理.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">CSS</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../CSS/18种CSS命名和书写规范.html">
            
                <a href="../CSS/18种CSS命名和书写规范.html">
            
                    
                    18种CSS命名和书写规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../CSS/灵活运用CSS开发技巧.html">
            
                <a href="../CSS/灵活运用CSS开发技巧.html">
            
                    
                    灵活运用CSS开发技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../CSS/自适应 flex grid.html">
            
                <a href="../CSS/自适应 flex grid.html">
            
                    
                    自适应 Flex Grid
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">HTML</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../HTML/浏览器里的本地数据库：IndexedDB.html">
            
                <a href="../HTML/浏览器里的本地数据库：IndexedDB.html">
            
                    
                    浏览器里的本地数据库：IndexedDB
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Vue</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../Vue/7个有用的Vue开发技巧.html">
            
                <a href="../Vue/7个有用的Vue开发技巧.html">
            
                    
                    7个有用的Vue开发技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../Vue/Vue this.$router.push、replace、go的区别.html">
            
                <a href="../Vue/Vue this.$router.push、replace、go的区别.html">
            
                    
                    Vue This.$Router.Push、Replace、Go的区别
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../Vue/Vue 的父组件和子组件生命周期钩子函数执行顺序？.html">
            
                <a href="../Vue/Vue 的父组件和子组件生命周期钩子函数执行顺序？.html">
            
                    
                    Vue 的父组件和子组件生命周期钩子函数执行顺序？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../Vue/Vue.set和this.$set应用的场景.html">
            
                <a href="../Vue/Vue.set和this.$set应用的场景.html">
            
                    
                    Vue.Set和This.$Set应用的场景
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../Vue/vue高级技巧.html">
            
                <a href="../Vue/vue高级技巧.html">
            
                    
                    Vue高级技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../Vue/从 0 开始手把手带你搭建一套规范的 Vue3.x 项目工程环境.html">
            
                <a href="../Vue/从 0 开始手把手带你搭建一套规范的 Vue3.x 项目工程环境.html">
            
                    
                    从 0 开始手把手带你搭建一套规范的 Vue3.X 项目工程环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../Vue/浅析 vue-router 源码和动态路由权限分配.html">
            
                <a href="../Vue/浅析 vue-router 源码和动态路由权限分配.html">
            
                    
                    浅析 Vue Router 源码和动态路由权限分配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="../Vue/父组件监听到子组件的生命周期.html">
            
                <a href="../Vue/父组件监听到子组件的生命周期.html">
            
                    
                    父组件监听到子组件的生命周期
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="../Vue/理解Vue.mixin，利用Vue.mixin正确的偷懒.html">
            
                <a href="../Vue/理解Vue.mixin，利用Vue.mixin正确的偷懒.html">
            
                    
                    理解Vue.Mixin，利用Vue.Mixin正确的偷懒
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">JavaScript</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="5个技巧让你更好的编写 JavaScript(ES6">
            
                <span>
            
                    
                    5个技巧让你更好的编写 JavaScript(ES6) 中条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="5种常见函数的写法和调用方式.html">
            
                <a href="5种常见函数的写法和调用方式.html">
            
                    
                    5种常见函数的写法和调用方式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="7道javascript题，让你理解基本的javascript.html">
            
                <a href="7道javascript题，让你理解基本的javascript.html">
            
                    
                    7道Javascript题，让你理解基本的Javascript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="Array.from(">
            
                <span>
            
                    
                    Array.From() 五个超好用的用途
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="ES6汇总资料.html">
            
                <a href="ES6汇总资料.html">
            
                    
                    ES6汇总资料
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="JS开发必须知道的41个技巧.html">
            
                <a href="JS开发必须知道的41个技巧.html">
            
                    
                    JS开发必须知道的41个技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="JavaScript之防抖Debounce和节流Throttle.html">
            
                <a href="JavaScript之防抖Debounce和节流Throttle.html">
            
                    
                    JavaScript之防抖Debounce和节流Throttle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="JavaScript数组求并集，交集和差集.html">
            
                <a href="JavaScript数组求并集，交集和差集.html">
            
                    
                    JavaScript数组求并集，交集和差集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="JavaScript的优雅写法.html">
            
                <a href="JavaScript的优雅写法.html">
            
                    
                    JavaScript的优雅写法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.10" data-path="axios知识点.html">
            
                <a href="axios知识点.html">
            
                    
                    Axios知识点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.11" data-path="javascript核心知识.html">
            
                <a href="javascript核心知识.html">
            
                    
                    Javascript核心知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.12" data-path="this、apply、call、bind.html">
            
                <a href="this、apply、call、bind.html">
            
                    
                    This、Apply、Call、Bind
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.13" data-path="前端js常用的60余种工具方法.html">
            
                <a href="前端js常用的60余种工具方法.html">
            
                    
                    前端Js常用的60余种工具方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.14" data-path="前端异常的捕获与处理.html">
            
                <a href="前端异常的捕获与处理.html">
            
                    
                    前端异常的捕获与处理
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="4.15" data-path="在 JavaScript 中交换值的 10 种方法.html">
            
                <a href="在 JavaScript 中交换值的 10 种方法.html">
            
                    
                    在 JavaScript 中交换值的 10 种方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.16" data-path="常用的前端JavaScript方法封装.html">
            
                <a href="常用的前端JavaScript方法封装.html">
            
                    
                    常用的前端JavaScript方法封装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.17" data-path="异步神器Async-await介绍与填坑.html">
            
                <a href="异步神器Async-await介绍与填坑.html">
            
                    
                    异步神器Async Await介绍与填坑
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.18" data-path="灵活运用JS开发技巧.html">
            
                <a href="灵活运用JS开发技巧.html">
            
                    
                    灵活运用JS开发技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.19" data-path="编写高质量可维护的代码：一目了然的注释.html">
            
                <a href="编写高质量可维护的代码：一目了然的注释.html">
            
                    
                    编写高质量可维护的代码：一目了然的注释
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.20" data-path="编写高质量可维护的代码：优化逻辑判断.html">
            
                <a href="编写高质量可维护的代码：优化逻辑判断.html">
            
                    
                    编写高质量可维护的代码：优化逻辑判断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.21" data-path="编写高质量可维护的代码：优雅命名.html">
            
                <a href="编写高质量可维护的代码：优雅命名.html">
            
                    
                    编写高质量可维护的代码：优雅命名
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.22" data-path="编写高质量可维护的代码：数据建模.html">
            
                <a href="编写高质量可维护的代码：数据建模.html">
            
                    
                    编写高质量可维护的代码：数据建模
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Life Exp</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../life_exp/告别平庸的15个小方法.html">
            
                <a href="../life_exp/告别平庸的15个小方法.html">
            
                    
                    告别平庸的15个小方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../life_exp/日志记录.html">
            
                <a href="../life_exp/日志记录.html">
            
                    
                    日志记录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../life_exp/程序员的发展方向.html">
            
                <a href="../life_exp/程序员的发展方向.html">
            
                    
                    程序员的发展方向
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../message/">
            
                <a href="../message/">
            
                    
                    Message
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Technology</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../technology/WebSocket.html">
            
                <a href="../technology/WebSocket.html">
            
                    
                    WebSocket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="../technology/npm常用命令大全.html">
            
                <a href="../technology/npm常用命令大全.html">
            
                    
                    Npm常用命令大全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="../technology/智能照明项目说明.html">
            
                <a href="../technology/智能照明项目说明.html">
            
                    
                    智能照明项目说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="../technology/规范化git的提交信息.html">
            
                <a href="../technology/规范化git的提交信息.html">
            
                    
                    规范化Git的提交信息
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Tutorial</li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="../tutorial/Esmap地图详解.html">
            
                <a href="../tutorial/Esmap地图详解.html">
            
                    
                    Esmap地图详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="../tutorial/npm镜像切换.html">
            
                <a href="../tutorial/npm镜像切换.html">
            
                    
                    Npm镜像切换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="../tutorial/发布npm的教程.html">
            
                <a href="../tutorial/发布npm的教程.html">
            
                    
                    发布Npm的教程
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >在 JavaScript 中交换值的 10 种方法</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <html><head></head><body><div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#&#x5728;-javascript-&#x4E2D;&#x4EA4;&#x6362;&#x503C;&#x7684;-10-&#x79CD;&#x65B9;&#x6CD5;"><b>1.1. </b>&#x5728; JavaScript &#x4E2D;&#x4EA4;&#x6362;&#x503C;&#x7684; 10 &#x79CD;&#x65B9;&#x6CD5;</a></li><li><span class="title-icon "></span><a href="#1-&#x4F7F;&#x7528;&#x4E34;&#x65F6;&#x53D8;&#x91CF;"><b>1.2. </b>1 &#x4F7F;&#x7528;&#x4E34;&#x65F6;&#x53D8;&#x91CF;</a></li><li><span class="title-icon "></span><a href="#2-&#x4F7F;&#x7528;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x7B26;--&#x548C;--"><b>1.3. </b>2 &#x4F7F;&#x7528;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x7B26; + &#x548C; -</a></li><li><span class="title-icon "></span><a href="#3-&#x4EC5;&#x4F7F;&#x7528;--&#x6216;---&#x8FD0;&#x7B97;&#x7B26;"><b>1.4. </b>3 &#x4EC5;&#x4F7F;&#x7528; + &#x6216; - &#x8FD0;&#x7B97;&#x7B26;</a></li><li><span class="title-icon "></span><a href="#4-&#x4F7F;&#x7528;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x7B26;--&#x548C;-"><b>1.5. </b>4 &#x4F7F;&#x7528;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x7B26; * &#x548C; /</a></li><li><span class="title-icon "></span><a href="#5-&#x4EC5;&#x4F7F;&#x7528;--&#x6216;--&#x8FD0;&#x7B97;&#x7B26;"><b>1.6. </b>5 &#x4EC5;&#x4F7F;&#x7528; * &#x6216; / &#x8FD0;&#x7B97;&#x7B26;</a></li><li><span class="title-icon "></span><a href="#6-&#x4F7F;&#x7528;&#x6309;&#x4F4D;&#x5F02;&#x6216;&#xFF08;xor&#xFF09;&#x3002;"><b>1.7. </b>6 &#x4F7F;&#x7528;&#x6309;&#x4F4D;&#x5F02;&#x6216;&#xFF08;XOR&#xFF09;&#x3002;</a></li><li><span class="title-icon "></span><a href="#7-&#x4F7F;&#x7528;&#x6309;&#x4F4D;&#x540C;&#x6216;-&#xFF08;xnor&#xFF09;"><b>1.8. </b>7 &#x4F7F;&#x7528;&#x6309;&#x4F4D;&#x540C;&#x6216; &#xFF08;XNOR&#xFF09;</a></li><li><span class="title-icon "></span><a href="#8-&#x5728;&#x6570;&#x7EC4;&#x4E2D;&#x8FDB;&#x884C;&#x8D4B;&#x503C;"><b>1.9. </b>8 &#x5728;&#x6570;&#x7EC4;&#x4E2D;&#x8FDB;&#x884C;&#x8D4B;&#x503C;</a></li><li><span class="title-icon "></span><a href="#9-&#x4F7F;&#x7528;&#x89E3;&#x6784;&#x8868;&#x8FBE;&#x5F0F;"><b>1.10. </b>9 &#x4F7F;&#x7528;&#x89E3;&#x6784;&#x8868;&#x8FBE;&#x5F0F;</a></li><li><span class="title-icon "></span><a href="#10&#x3001;&#x4F7F;&#x7528;&#x7ACB;&#x5373;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;iife&#xFF09;"><b>1.11. </b>10&#x3001;&#x4F7F;&#x7528;&#x7ACB;&#x5373;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;IIFE&#xFF09;</a></li></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h2 id="&#x5728;-javascript-&#x4E2D;&#x4EA4;&#x6362;&#x503C;&#x7684;-10-&#x79CD;&#x65B9;&#x6CD5;"><a name="&#x5728;-javascript-&#x4E2D;&#x4EA4;&#x6362;&#x503C;&#x7684;-10-&#x79CD;&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#&#x5728;-javascript-&#x4E2D;&#x4EA4;&#x6362;&#x503C;&#x7684;-10-&#x79CD;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. &#x5728; JavaScript &#x4E2D;&#x4EA4;&#x6362;&#x503C;&#x7684; 10 &#x79CD;&#x65B9;&#x6CD5;</h2>
<p>&#x5728;&#x5F00;&#x53D1;&#x8FC7;&#x7A0B;&#x4E2D;&#x53C8;&#x662F;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5BF9;&#x503C;&#x8FDB;&#x884C;&#x4EA4;&#x6362;&#x3002;&#x4E00;&#x822C;&#x6211;&#x4EEC;&#x90FD;&#x5728;&#x7528;&#x4E00;&#x79CD;&#x7B80;&#x5355;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#xFF1A;&#x201C;&#x4E34;&#x65F6;&#x53D8;&#x91CF;&#x201D;&#x3002;&#x4E0D;&#x8FC7;&#x8FD8;&#x6709;&#x66F4;&#x597D;&#x7684;&#x529E;&#x6CD5;&#xFF0C;&#x800C;&#x4E14;&#x4E0D;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#xFF0C;&#x6709;&#x5F88;&#x591A;&#x3002;&#x6709;&#x65F6;&#x6211;&#x4EEC;&#x5728;&#x7F51;&#x4E0A;&#x641C;&#x5BFB;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#xFF0C;&#x627E;&#x5230;&#x540E;&#x590D;&#x5236;&#x7C98;&#x8D34;&#xFF0C;&#x4F46;&#x662F;&#x4ECE;&#x6CA1;&#x60F3;&#x8FC7;&#x8FD9;&#x5C0F;&#x6BB5;&#x4EE3;&#x7801;&#x662F;&#x600E;&#x6837;&#x5DE5;&#x4F5C;&#x7684;&#x3002;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x8BE5;&#x5B66;&#x4E60;&#x4E00;&#x4E0B;&#x5E94;&#x8BE5;&#x600E;&#x6837;&#x8F7B;&#x677E;&#x9AD8;&#x6548;&#x5730;&#x4EA4;&#x6362;&#x503C;&#x4E86;&#x3002;</p>
<h2 id="1-&#x4F7F;&#x7528;&#x4E34;&#x65F6;&#x53D8;&#x91CF;"><a name="1-&#x4F7F;&#x7528;&#x4E34;&#x65F6;&#x53D8;&#x91CF;" class="anchor-navigation-ex-anchor" href="#1-&#x4F7F;&#x7528;&#x4E34;&#x65F6;&#x53D8;&#x91CF;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. 1 &#x4F7F;&#x7528;&#x4E34;&#x65F6;&#x53D8;&#x91CF;</h2>
<p>&#x5148;&#x662F;&#x6700;&#x7B80;&#x5355;&#x7684;&#x4E00;&#x79CD;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithTemp</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  <span class="hljs-keyword">var</span> temp = num1;
  num1 = num2;
  num2 = temp;

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

swapWithTemp(<span class="hljs-number">2.34</span>,<span class="hljs-number">3.45</span>)
</code></pre>
<h2 id="2-&#x4F7F;&#x7528;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x7B26;--&#x548C;--"><a name="2-&#x4F7F;&#x7528;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x7B26;--&#x548C;--" class="anchor-navigation-ex-anchor" href="#2-&#x4F7F;&#x7528;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x7B26;--&#x548C;--"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. 2 &#x4F7F;&#x7528;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x7B26; + &#x548C; -</h2>
<p>&#x8FD8;&#x53EF;&#x4EE5;&#x7528;&#x4E00;&#x4E9B;&#x6570;&#x5B66;&#x9B54;&#x672F;&#x6765;&#x4EA4;&#x6362;&#x503C;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithPlusMinus</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num1 = num1+num2;
  num2 = num1-num2;
  num1 = num1-num2;

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

swapWithPlusMinus(<span class="hljs-number">2.34</span>,<span class="hljs-number">3.45</span>)
</code></pre>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x770B;&#x5B83;&#x662F;&#x5982;&#x4F55;&#x5DE5;&#x4F5C;&#x7684;&#x3002;&#x6211;&#x4EEC;&#x5728;&#x7B2C; 4 &#x884C;&#x83B7;&#x5F97;&#x4E24;&#x4E2A;&#x6570;&#x5B57;&#x7684;&#x603B;&#x548C;&#x3002;&#x73B0;&#x5728;&#xFF0C;&#x5982;&#x679C;&#x4ECE;&#x548C;&#x4E2D;&#x51CF;&#x53BB;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#xFF0C;&#x90A3;&#x4E48;&#x53E6;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#x5C31;&#x6B63;&#x786E;&#x4E86;&#x3002;&#x8FD9;&#x5C31;&#x662F;&#x7B2C; 5 &#x884C;&#x6240;&#x505A;&#x7684;&#x5DE5;&#x4F5C;&#x3002;&#x4ECE;&#x5B58;&#x50A8;&#x5728; <code>num1</code> &#x53D8;&#x91CF;&#x4E2D;&#x7684;&#x603B;&#x548C;&#x4E2D;&#x51CF;&#x53BB; <code>num2</code> &#x4F1A;&#x5F97;&#x5230;&#x5B58;&#x50A8;&#x5728; <code>num2</code> &#x4E2D;&#x7684;&#x539F;&#x59CB; <code>num1</code> &#x503C;&#x3002;&#x540C;&#x6837;&#xFF0C;&#x5728;&#x7B2C; 6 &#x884C;&#x7684; <code>num1</code> &#x4E2D;&#x5F97;&#x5230; <code>num2</code> &#x7684;&#x503C;&#x3002;</p>
<p><strong>&#x5C0F;&#x5FC3;</strong>&#xFF1A;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x4E0E; <code>+</code> &#x548C; <code>-</code> &#x4E92;&#x6362;&#x7684;&#x5355;&#x884C;&#x4EE3;&#x7801;&#x65B9;&#x6848;&#xFF0C;&#x4E0D;&#x8FC7;&#x3002;&#x3002;&#x3002;</p>
<p>&#x5B83;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithPlusMinusShort</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num2 = num1+(num1=num2)-num2;

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

swapWithPlusMinusShort(<span class="hljs-number">2</span>,<span class="hljs-number">3</span>)
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x7ED9;&#x51FA;&#x4E86;&#x9884;&#x671F;&#x7684;&#x7ED3;&#x679C;&#x3002; <code>()</code> &#x4E2D;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x5C06; <code>num2</code> &#x5B58;&#x50A8;&#x5728; <code>num1</code> &#x4E2D;&#xFF0C;&#x7136;&#x540E;&#x51CF;&#x53BB; <code>num1 - num2</code>&#xFF0C;&#x9664;&#x4E86;&#x51CF;&#x53BB; <code>num2 - num2 = 0</code> &#x4E4B;&#x5916;&#x4EC0;&#x4E48;&#x4E5F;&#x6CA1;&#x6709;&#x505A;&#xFF0C;&#x56E0;&#x6B64;&#x5F97;&#x5230;&#x4E86;&#x7ED3;&#x679C;&#x3002;&#x4F46;&#x662F;&#x5F53;&#x4F7F;&#x7528;&#x6D6E;&#x70B9;&#x6570;&#x65F6;&#xFF0C;&#x4F1A;&#x770B;&#x5230;&#x4E00;&#x4E9B;&#x610F;&#x5916;&#x7684;&#x7ED3;&#x679C;&#x3002;</p>
<p>&#x8BD5;&#x7740;&#x6267;&#x884C;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x5E76;&#x67E5;&#x770B;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithPlusMinusShort</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num2 = num1+(num1=num2)-num2;

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

swapWithPlusMinusShort(<span class="hljs-number">2</span>,<span class="hljs-number">3.1</span>)
</code></pre>
<h2 id="3-&#x4EC5;&#x4F7F;&#x7528;--&#x6216;---&#x8FD0;&#x7B97;&#x7B26;"><a name="3-&#x4EC5;&#x4F7F;&#x7528;--&#x6216;---&#x8FD0;&#x7B97;&#x7B26;" class="anchor-navigation-ex-anchor" href="#3-&#x4EC5;&#x4F7F;&#x7528;--&#x6216;---&#x8FD0;&#x7B97;&#x7B26;"><i class="fa fa-link" aria-hidden="true"></i></a>1.4. 3 &#x4EC5;&#x4F7F;&#x7528; + &#x6216; - &#x8FD0;&#x7B97;&#x7B26;</h2>
<p>&#x4EC5;&#x901A;&#x8FC7;&#x4F7F;&#x7528; <code>+</code> &#x8FD0;&#x7B97;&#x7B26;&#x5C31;&#x53EF;&#x4EE5;&#x8FBE;&#x5230;&#x540C;&#x65F6;&#x4F7F;&#x7528; <code>+</code> &#x548C; <code>-</code> &#x76F8;&#x540C;&#x7684;&#x7ED3;&#x679C;&#x3002;</p>
<p>&#x770B;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithPlus</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num2 = num1 + (num1=num2, <span class="hljs-number">0</span>)

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

<span class="hljs-comment">//Try with - operator</span>
swapWithPlus(<span class="hljs-number">2.3</span>,<span class="hljs-number">3.4</span>)
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x662F;&#x6709;&#x6548;&#x7684;&#xFF0C;&#x4F46;&#x727A;&#x7272;&#x4E86;&#x53EF;&#x8BFB;&#x6027;&#x3002;&#x5728;&#x7B2C; 4 &#x884C;&#x7684; <code>()</code> &#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06; <code>num1</code> &#x8D4B;&#x503C;&#x7ED9; <code>num2</code>&#xFF0C;&#x800C;&#x65C1;&#x8FB9;&#x7684; <code>0</code> &#x662F;&#x8FD4;&#x56DE;&#x503C;&#x3002;&#x7B80;&#x800C;&#x8A00;&#x4E4B;&#xFF0C;&#x7B2C; 4 &#x884C;&#x7684;&#x8FD0;&#x7B97;&#x903B;&#x8F91;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-javascript">num2 = num1 + <span class="hljs-number">0</span> 
=&gt; num2 = num1.
</code></pre>
<p>&#x6240;&#x4EE5;&#x5F97;&#x5230;&#x4E86;&#x6B63;&#x786E;&#x7ED3;&#x679C;&#x3002;</p>
<p><strong>&#x6CE8;&#x610F;</strong>&#xFF1A;&#x4E00;&#x4E9B; JavaScript &#x5F15;&#x64CE;&#x53EF;&#x80FD;&#x4F1A;&#x5BF9;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x8FDB;&#x884C;&#x4F18;&#x5316;&#xFF0C;&#x4ECE;&#x800C;&#x5FFD;&#x7565; <code>+ 0</code>&#x3002;</p>
<h2 id="4-&#x4F7F;&#x7528;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x7B26;--&#x548C;-"><a name="4-&#x4F7F;&#x7528;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x7B26;--&#x548C;-" class="anchor-navigation-ex-anchor" href="#4-&#x4F7F;&#x7528;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x7B26;--&#x548C;-"><i class="fa fa-link" aria-hidden="true"></i></a>1.5. 4 &#x4F7F;&#x7528;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x7B26; * &#x548C; /</h2>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x7528; <code>*</code> &#x548C;<code>/</code> &#x8FD0;&#x7B97;&#x7B26;&#x73A9;&#x66F4;&#x591A;&#x7684;&#x82B1;&#x6837;&#x3002;</p>
<p>&#x5176;&#x539F;&#x7406;&#x4E0E;&#x5148;&#x524D;&#x7684;&#x65B9;&#x6CD5;&#x76F8;&#x540C;&#xFF0C;&#x4F46;&#x662F;&#x6709;&#x4E00;&#x4E9B;&#x5C0F;&#x95EE;&#x9898;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithMulDiv</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num1 = num1*num2;
  num2 = num1/num2;
  num1 = num1/num2;

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

swapWithMulDiv(<span class="hljs-number">2.34</span>,<span class="hljs-number">3.45</span>)
</code></pre>
<p>&#x4E0E;&#x4E0A;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x76F8;&#x540C;&#x3002;&#x9996;&#x5148;&#x5F97;&#x5230;&#x4E24;&#x4E2A;&#x6570;&#x5B57;&#x7684;&#x4E58;&#x79EF;&#xFF0C;&#x5E76;&#x5C06;&#x5B83;&#x4EEC;&#x5B58;&#x50A8;&#x5728; <code>num1</code> &#x4E2D;&#x3002;&#x7136;&#x540E;&#x5728;&#x7B2C; 5 &#x884C;&#xFF0C;&#x628A; <code>num2</code> &#x4E0E;&#x8FD9;&#x4E2A;&#x7ED3;&#x679C;&#x76F8;&#x9664;&#xFF0C;&#x5F97;&#x5230;&#x7B2C;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#xFF0C;&#x7136;&#x540E;&#x91CD;&#x590D;&#x6B64;&#x8FC7;&#x7A0B;&#x4EE5;&#x83B7;&#x5F97;&#x7B2C;&#x4E8C;&#x4E2A;&#x6570;&#x5B57;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x4F60;&#x6210;&#x201C; <strong>&#x6570;&#x5B66;&#x5BB6;</strong>&#x201D; &#x4E86;&#x3002;</p>
<p>&#x4E0D;&#x8FC7;&#x90A3;&#x5C0F;&#x95EE;&#x9898;&#x5728;&#x54EA;&#x513F;&#x5462;&#xFF1F;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x5C1D;&#x8BD5;&#x4E00;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithMulDiv</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num1 = num1*num2;
  num2 = num1/num2;
  num1 = num1/num2;

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

<span class="hljs-comment">//&#x8BD5;&#x7740;&#x6539;&#x53D8;&#x6570;&#x5B57;&#x7684;&#x503C;&#xFF0C;&#x770B;&#x770B;&#x4F1A;&#x53D1;&#x751F;&#x4EC0;&#x4E48;</span>
swapWithMulDiv(<span class="hljs-number">2.34</span>,<span class="hljs-number">0</span>)
</code></pre>
<p>&#x6211;&#x4EEC;&#x7684;&#x503C;&#x6CA1;&#x6709;&#x4EA4;&#x6362;&#xFF0C;&#x800C;&#x662F;&#x5F97;&#x5230;&#x4E86;&#x4E00;&#x4E2A;&#x5947;&#x602A;&#x7684; <code>NaN</code>&#xFF0C;&#x8FD9;&#x662F;&#x600E;&#x4E48;&#x56DE;&#x4E8B;&#x3002;&#x5982;&#x679C;&#x4F60;&#x8FD8;&#x8BB0;&#x5F97;&#x5C0F;&#x5B66;&#x7684;&#x6570;&#x5B66;&#x8BFE;&#xFF0C;&#x5C31;&#x4F1A;&#x60F3;&#x8D77;&#x4E0D;&#x8981;&#x9664;&#x4EE5; 0&#xFF0C;&#x56E0;&#x4E3A;&#x90A3;&#x662F;&#x6CA1;&#x6709;&#x610F;&#x4E49;&#x7684;&#x3002;</p>
<p>&#x7136;&#x540E;&#x518D;&#x770B;&#x770B;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#x7684;&#x5176;&#x4ED6;&#x95EE;&#x9898;&#xFF0C;&#x770B;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithMulDiv</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num1 = num1*num2;
  num2 = num1/num2;
  num1 = num1/num2;

  <span class="hljs-built_in">console</span>.log(num1,num2)
}
<span class="hljs-comment">//&#x770B;&#x770B;&#x4F1A;&#x53D1;&#x751F;&#x4EC0;&#x4E48;</span>
swapWithMulDiv(<span class="hljs-number">2.34</span>,<span class="hljs-literal">Infinity</span>)
</code></pre>
<p>&#x6CA1;&#x9519;&#xFF0C;&#x53C8;&#x662F; <strong>NaN</strong>&#x3002;&#x56E0;&#x4E3A;&#x4F60;&#x65E0;&#x6CD5;&#x4F7F;&#x7528; <code>Infinity</code> &#x53BB;&#x9664;&#x4EFB;&#x4F55;&#x503C;&#xFF0C;&#x5B83;&#x662F;&#x672A;&#x5B9A;&#x4E49;&#x7684;&#x3002;</p>
<p>&#x4F46;&#x6211;&#x8FD8;&#x60F3;&#x518D;&#x8BD5;&#x8BD5;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithMulDiv</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num1 = num1*num2;
  num2 = num1/num2;
  num1 = num1/num2;

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

<span class="hljs-comment">//&#x4F1A;&#x600E;&#x6837;&#x5462;</span>
swapWithMulDiv(<span class="hljs-number">2.34</span>,-<span class="hljs-literal">Infinity</span>)
</code></pre>
<p><strong>-Infinity</strong> &#x7684;&#x7ED3;&#x679C;&#x4E0E;&#x524D;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x76F8;&#x540C;&#xFF0C;&#x539F;&#x56E0;&#x4E5F;&#x4E00;&#x6837;&#x3002;</p>
<p>&#x4E8B;&#x5B9E;&#x8BC1;&#x660E;&#xFF0C;&#x5373;&#x4F7F;&#x4F60;&#x662F;&#x4E00;&#x4F4D;&#x51FA;&#x8272;&#x7684;&#x201C;<strong>&#x6570;&#x5B66;&#x5BB6;</strong>&#x201D;&#xFF0C;&#x4E5F;&#x6709;&#x65E0;&#x80FD;&#x4E3A;&#x529B;&#x7684;&#x65F6;&#x5019;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x7528; <code>*</code> &#x548C; <code>/</code> &#x8FDB;&#x884C;&#x503C;&#x4EA4;&#x6362;&#x7684;&#x8F83;&#x77ED;&#x7248;&#x672C;&#xFF0C;&#x4ECD;&#x5B58;&#x5728;&#x76F8;&#x540C;&#x7684;&#x95EE;&#x9898;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithMulDivShort</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num2 = num1*(num1=num2)/num2;

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

swapWithMulDivShort(<span class="hljs-number">2.3</span>,<span class="hljs-number">3.4</span>)
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x7C7B;&#x4F3C;&#x4E8E;&#x7528; <code>+</code> &#x548C; <code>-</code> &#x8FDB;&#x884C;&#x4EA4;&#x6362;&#x65F6;&#x7684;&#x8F83;&#x77ED;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x628A; <code>num2</code> &#x8D4B;&#x503C;&#x7ED9; <code>num1</code>&#xFF0C;&#x7136;&#x540E;&#x7B2C; 4 &#x884C;&#x7684;&#x6F14;&#x7B97;&#x903B;&#x8F91;&#x662F;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-javascript">num2 = num1 * num2 / <span class="hljs-function"><span class="hljs-params">num2</span>
    =&gt;</span> num2 = num1
</code></pre>
<p>&#x8FD9;&#x6837;&#x4E24;&#x4E2A;&#x503C;&#x5C31;&#x4E92;&#x6362;&#x4E86;&#x3002;</p>
<h2 id="5-&#x4EC5;&#x4F7F;&#x7528;--&#x6216;--&#x8FD0;&#x7B97;&#x7B26;"><a name="5-&#x4EC5;&#x4F7F;&#x7528;--&#x6216;--&#x8FD0;&#x7B97;&#x7B26;" class="anchor-navigation-ex-anchor" href="#5-&#x4EC5;&#x4F7F;&#x7528;--&#x6216;--&#x8FD0;&#x7B97;&#x7B26;"><i class="fa fa-link" aria-hidden="true"></i></a>1.6. 5 &#x4EC5;&#x4F7F;&#x7528; * &#x6216; / &#x8FD0;&#x7B97;&#x7B26;</h2>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithMul</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num2 = num1 * (num1=num2, <span class="hljs-number">1</span>)

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

<span class="hljs-comment">//Try with / and ** operator</span>
swapWithMul(<span class="hljs-number">2.3</span>,<span class="hljs-number">3.4</span>)
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x7A0B;&#x5E8F;&#x662F;&#x6709;&#x6548;&#x7684;&#xFF0C;&#x4F46;&#x727A;&#x7272;&#x4E86;&#x53EF;&#x8BFB;&#x6027;&#x3002;&#x5728;&#x7B2C; 4 &#x884C;&#x7684; <code>()</code> &#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06; <code>num1</code> &#x8D4B;&#x503C;&#x7ED9; <code>num2</code>&#xFF0C;&#x65C1;&#x8FB9;&#x7684; <code>1</code> &#x662F;&#x8FD4;&#x56DE;&#x503C;&#x3002;&#x7B80;&#x800C;&#x8A00;&#x4E4B;&#xFF0C;&#x7B2C; 4 &#x884C;&#x7684;&#x903B;&#x8F91;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-javascript">num2 = num1 * <span class="hljs-number">1</span> 
    =&gt; num2 = num1
</code></pre>
<p>&#x8FD9;&#x6837;&#x5C31;&#x5F97;&#x5230;&#x4E86;&#x7ED3;&#x679C;&#x3002;</p>
<h2 id="6-&#x4F7F;&#x7528;&#x6309;&#x4F4D;&#x5F02;&#x6216;&#xFF08;xor&#xFF09;&#x3002;"><a name="6-&#x4F7F;&#x7528;&#x6309;&#x4F4D;&#x5F02;&#x6216;&#xFF08;xor&#xFF09;&#x3002;" class="anchor-navigation-ex-anchor" href="#6-&#x4F7F;&#x7528;&#x6309;&#x4F4D;&#x5F02;&#x6216;&#xFF08;xor&#xFF09;&#x3002;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7. 6 &#x4F7F;&#x7528;&#x6309;&#x4F4D;&#x5F02;&#x6216;&#xFF08;XOR&#xFF09;&#x3002;</h2>
<p>XOR &#x7528;&#x6765;&#x8FDB;&#x884C;&#x4E8C;&#x8FDB;&#x5236;&#x4F4D;&#x8FD0;&#x7B97;&#x3002;&#x5F53;&#x6709;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x8F93;&#x5165;&#x65F6;&#xFF0C;&#x5B83;&#x7684;&#x7ED3;&#x679C;&#x4E3A; 1&#xFF0C;&#x5426;&#x5219;&#x4E3A; 0&#x3002;</p>
<table>
<thead>
<tr>
<th style="text-align:center">X</th>
<th style="text-align:center">Y</th>
<th style="text-align:center">X^Y</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">1</td>
<td style="text-align:center">1</td>
<td style="text-align:center">0</td>
</tr>
<tr>
<td style="text-align:center">1</td>
<td style="text-align:center">0</td>
<td style="text-align:center">1</td>
</tr>
<tr>
<td style="text-align:center">0</td>
<td style="text-align:center">1</td>
<td style="text-align:center">1</td>
</tr>
<tr>
<td style="text-align:center">0</td>
<td style="text-align:center">0</td>
<td style="text-align:center">0</td>
</tr>
</tbody>
</table>
<p>&#x5148;&#x4E86;&#x89E3;&#x5176;&#x5DE5;&#x4F5C;&#x539F;&#x7406;&#xFF01;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithXOR</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num1 = num1^num2;
  num2 = num1^num2; 
  num1 = num1^num2;

  <span class="hljs-built_in">console</span>.log(num1,num2)
}
<span class="hljs-comment">// &#x8BD5;&#x8BD5;&#x8D1F;&#x503C;&#x4F1A;&#x600E;&#x6837;</span>
swapWithXOR(<span class="hljs-number">10</span>,<span class="hljs-number">1</span>)
</code></pre>
<p>10 &#x7684;4 &#x4F4D;&#x4E8C;&#x8FDB;&#x5236;&#x6570; -&gt; 1010</p>
<p>1 &#x7684; 4 &#x4F4D;&#x4E8C;&#x8FDB;&#x5236;&#x6570; -&gt; 0001</p>
<p>&#x73B0;&#x5728;&#xFF1A;</p>
<pre><code class="lang-javascript">&#x7B2C;&#x56DB;&#x884C;: 
num1 = num1 ^ <span class="hljs-function"><span class="hljs-params">num2</span> 
    =&gt;</span> <span class="hljs-number">1010</span> ^ <span class="hljs-number">0001</span> 
    =&gt; <span class="hljs-number">1011</span> 
    =&gt; <span class="hljs-number">7</span> 
&#x7B2C;&#x4E94;&#x884C;: 
num2 = num1 ^ <span class="hljs-function"><span class="hljs-params">num2</span> 
    =&gt;</span> <span class="hljs-number">1011</span> ^ <span class="hljs-number">0001</span> 
    =&gt; <span class="hljs-number">1010</span> 
    =&gt; <span class="hljs-number">10</span>
&#x7B2C;&#x516D;&#x884C;: 
num1 = num1 ^ <span class="hljs-function"><span class="hljs-params">num2</span> 
    =&gt;</span> <span class="hljs-number">1011</span> ^ <span class="hljs-number">1010</span> 
    =&gt; <span class="hljs-number">0001</span> 
    =&gt; <span class="hljs-number">1</span>
</code></pre>
<p>&#x4E24;&#x4E2A;&#x503C;&#x4EA4;&#x6362;&#x4E86;&#x3002;</p>
<p>&#x518D;&#x6765;&#x770B;&#x53E6;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithXOR</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num1 = num1^num2;
  num2 = num1^num2;
  num1 = num1^num2;

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

swapWithXOR(<span class="hljs-number">2.34</span>,<span class="hljs-number">3.45</span>)
</code></pre>
<p>&#x55EF;&#xFF1F;&#xFF1F;&#x4EA4;&#x6362;&#x7684;&#x503C;&#x5728;&#x54EA;&#x513F;&#xFF1F;&#x6211;&#x4EEC;&#x53EA;&#x662F;&#x5F97;&#x5230;&#x4E86;&#x6570;&#x5B57;&#x7684;&#x6574;&#x6570;&#x90E8;&#x5206;&#xFF0C;&#x8FD9;&#x5C31;&#x662F;&#x95EE;&#x9898;&#x6240;&#x5728;&#x3002; XOR &#x5047;&#x5B9A;&#x8F93;&#x5165;&#x662F;&#x6574;&#x6570;&#xFF0C;&#x6240;&#x4EE5;&#xB7;&#xB7;&#xB7;&#x76F8;&#x5E94;&#x5730;&#x6267;&#x884C;&#x8BA1;&#x7B97;&#x3002;&#x4F46;&#x662F;&#x6D6E;&#x70B9;&#x6570;&#x4E0D;&#x662F;&#x6574;&#x6570;&#xFF0C;&#x800C;&#x662F;&#x7531; IEEE 754 &#x6807;&#x51C6;&#x8868;&#x793A;&#x7684;&#xFF0C;&#x5C06;&#x6570;&#x5B57;&#x5206;&#x4E3A;&#x4E09;&#x90E8;&#x5206;&#xFF1A;<strong>&#x7B26;&#x53F7;</strong>&#x4F4D;&#x3001;&#x4EE3;&#x8868;<strong>&#x6307;&#x6570;</strong>&#x7684;&#x4E00;&#x7EC4;&#x4F4D;&#x548C;&#x4EE3;&#x8868;<strong>&#x5C3E;&#x6570;</strong>&#x7684;&#x4E00;&#x7EC4;&#x4F4D;&#x3002;&#x4F4D;&#x6570;&#x662F;&#x4ECB;&#x4E8E;1&#xFF08;&#x542B;&#xFF09;&#x548C;2&#xFF08;&#x4E0D;&#x542B;&#xFF09;&#x4E4B;&#x95F4;&#x7684;&#x6570;&#x5B57;&#x3002;&#x6240;&#x4EE5;&#x5F97;&#x5230;&#x7684;&#x503C;&#x4E0D;&#x6B63;&#x786E;&#x3002;</p>
<p>&#x53E6;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithXOR</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num1 = num1^num2;
  num2 = num1^num2;
  num1 = num1^num2;

  <span class="hljs-built_in">console</span>.log(num1,num2)
}
<span class="hljs-comment">// &#x8BD5;&#x8BD5; infinities &#x548C;&#x6574;&#x6570;&#x503C;.</span>
swapWithXOR(-<span class="hljs-literal">Infinity</span>,<span class="hljs-literal">Infinity</span>)
</code></pre>
<p>&#x6BEB;&#x65E0;&#x610F;&#x5916;&#xFF0C;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x5F97;&#x5230;&#x9884;&#x671F;&#x7684;&#x7ED3;&#x679C;&#x3002;&#x8FD9;&#x662F;&#x56E0;&#x4E3A; <strong>Infinity</strong> &#x548C; <strong>&#x2013; Infinity</strong> &#x90FD;&#x662F;&#x6D6E;&#x70B9;&#x6570;&#x3002;&#x6B63;&#x5982;&#x6211;&#x4EEC;&#x5728;&#x524D;&#x9762;&#x6240;&#x8BA8;&#x8BBA;&#x7684;&#xFF0C;&#x5BF9;&#x4E8E; XOR&#xFF0C;&#x6D6E;&#x70B9;&#x6570;&#x662F;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#x3002;</p>
<h2 id="7-&#x4F7F;&#x7528;&#x6309;&#x4F4D;&#x540C;&#x6216;-&#xFF08;xnor&#xFF09;"><a name="7-&#x4F7F;&#x7528;&#x6309;&#x4F4D;&#x540C;&#x6216;-&#xFF08;xnor&#xFF09;" class="anchor-navigation-ex-anchor" href="#7-&#x4F7F;&#x7528;&#x6309;&#x4F4D;&#x540C;&#x6216;-&#xFF08;xnor&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>1.8. 7 &#x4F7F;&#x7528;&#x6309;&#x4F4D;&#x540C;&#x6216; &#xFF08;XNOR&#xFF09;</h2>
<p>&#x5B83;&#x7528;&#x6765;&#x8FDB;&#x884C;&#x4E8C;&#x8FDB;&#x5236;&#x4F4D;&#x8FD0;&#x7B97;&#xFF0C;&#x4F46;&#x662F;&#x4E0E; XOR &#x6B63;&#x597D;&#x76F8;&#x53CD;&#x3002;&#x5F53;&#x6709;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x8F93;&#x5165;&#x65F6;&#xFF0C;XNOR &#x7684;&#x7ED3;&#x679C;&#x662F; 0&#xFF0C;&#x5426;&#x5219;&#x7ED3;&#x679C;&#x4E3A; 1&#x3002; JavaScript &#x6CA1;&#x6709;&#x6267;&#x884C; XNOR &#x7684;&#x8FD0;&#x7B97;&#x7B26;&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x7528; <strong>NOT</strong> &#x8FD0;&#x7B97;&#x7B26;&#x5BF9; XOR &#x7684;&#x7ED3;&#x679C;&#x6C42;&#x53CD;&#x3002;</p>
<table>
<thead>
<tr>
<th style="text-align:center">X</th>
<th style="text-align:center">Y</th>
<th style="text-align:center">XNOR</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">1</td>
<td style="text-align:center">1</td>
<td style="text-align:center">1</td>
</tr>
<tr>
<td style="text-align:center">1</td>
<td style="text-align:center">0</td>
<td style="text-align:center">0</td>
</tr>
<tr>
<td style="text-align:center">0</td>
<td style="text-align:center">1</td>
<td style="text-align:center">0</td>
</tr>
<tr>
<td style="text-align:center">0</td>
<td style="text-align:center">0</td>
<td style="text-align:center">1</td>
</tr>
</tbody>
</table>
<p>&#x5148;&#x4E86;&#x89E3;&#x5176;&#x5DE5;&#x4F5C;&#x539F;&#x7406;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithXNOR</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num1 = ~(num1^num2);
  num2 = ~(num1^num2);
  num1 = ~(num1^num2);

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

<span class="hljs-comment">//&#x53EF;&#x4EE5;&#x8BD5;&#x8BD5;&#x8D1F;&#x503C;</span>
swapWithXNOR(<span class="hljs-number">10</span>,<span class="hljs-number">1</span>)
</code></pre>
<p>10 &#x7684; 4 &#x4F4D;&#x4E8C;&#x8FDB;&#x5236;&#x6570; -&gt; 1010</p>
<p>1 &#x7684; 4 &#x4F4D;&#x4E8C;&#x8FDB;&#x5236;&#x6570; -&gt; 0001</p>
<p><strong>&#x7B2C; 4 &#x884C;&#xFF1A;</strong></p>
<pre><code class="lang-javascript">num1 = ~<span class="hljs-function">(<span class="hljs-params">num1 ^ num2</span>) 
    =&gt;</span> ~<span class="hljs-function">(<span class="hljs-params"><span class="hljs-number">1010</span> ^ <span class="hljs-number">0001</span></span>) 
    =&gt;</span>~<span class="hljs-function">(<span class="hljs-params"><span class="hljs-number">1011</span></span>) 
    =&gt;</span> ~<span class="hljs-number">11</span> 
    =&gt; <span class="hljs-number">-12</span>
</code></pre>
<p>&#x7531;&#x4E8E;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x8D1F;&#x6570;&#xFF0C;&#x6240;&#x4EE5;&#x9700;&#x8981;&#x5C06;&#x5176;&#x8F6C;&#x6362;&#x56DE;&#x4E8C;&#x8FDB;&#x5236;&#x5E76;&#x8BA1;&#x7B97; 2 &#x7684;&#x8865;&#x7801;&#x6765;&#x83B7;&#x53D6;&#x5341;&#x8FDB;&#x5236;&#x503C;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-number">-12</span> =&gt; <span class="hljs-number">1100</span> 
    =&gt; <span class="hljs-number">0011</span> + <span class="hljs-number">1</span> 
    =&gt; <span class="hljs-number">0100</span>
</code></pre>
<p><strong>&#x7B2C; 5 &#x884C;&#xFF1A;</strong></p>
<pre><code class="lang-javascript">num2 = ~<span class="hljs-function">(<span class="hljs-params">num1 ^ num2</span>) 
    =&gt;</span> ~<span class="hljs-function">(<span class="hljs-params"><span class="hljs-number">0100</span> ^ <span class="hljs-number">0001</span></span>) 
    =&gt;</span> ~<span class="hljs-function">(<span class="hljs-params"><span class="hljs-number">0101</span></span>) 
    =&gt;</span> ~<span class="hljs-number">5</span> 
    =&gt; <span class="hljs-number">-6</span><span class="hljs-number">-6</span> 
    =&gt; <span class="hljs-number">0110</span> 
    =&gt; <span class="hljs-number">1001</span> + <span class="hljs-number">1</span>
    =&gt; <span class="hljs-number">1010</span> 
    =&gt; <span class="hljs-number">10</span>
</code></pre>
<p><strong>&#x7B2C; 6 &#x884C;&#xFF1A;</strong></p>
<pre><code class="lang-javascript">num1 = ~<span class="hljs-function">(<span class="hljs-params">num1 ^ num2</span>) 
    =&gt;</span> ~<span class="hljs-function">(<span class="hljs-params"><span class="hljs-number">0100</span>^ <span class="hljs-number">1010</span></span>) 
    =&gt;</span> ~<span class="hljs-function">(<span class="hljs-params"><span class="hljs-number">1110</span></span>) 
    =&gt;</span> ~<span class="hljs-number">14</span> 
    =&gt; <span class="hljs-number">-15</span><span class="hljs-number">-15</span> 
    =&gt; <span class="hljs-number">1111</span> 
    =&gt; <span class="hljs-number">0000</span> + <span class="hljs-number">1</span> 
    =&gt; <span class="hljs-number">0001</span> 
    =&gt; <span class="hljs-number">1</span>
</code></pre>
<p>&#x82B1;&#x4E86;&#x4E00;&#x4E9B;&#x65F6;&#x95F4;&#xFF0C;&#x4F46;&#x8FD8;&#x662F;&#x4EA4;&#x6362;&#x4E86;&#x503C;&#x3002;&#x4F46;&#x4E0D;&#x5E78;&#x7684;&#x662F;&#xFF0C;&#x5B83;&#x9047;&#x5230;&#x4E86;&#x4E0E; XOR &#x76F8;&#x540C;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x4E0D;&#x80FD;&#x5904;&#x7406;&#x6D6E;&#x70B9;&#x6570;&#x548C;&#x65E0;&#x7A77;&#x5927;&#x3002;</p>
<p>&#x8BD5;&#x8BD5;&#x4E0B;&#x9762;&#x7684;&#x503C;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithXNOR</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num1 = ~(num1^num2);
  num2 = ~(num1^num2);
  num1 = ~(num1^num2);

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

swapWithXNOR(<span class="hljs-number">2.3</span>,<span class="hljs-number">4.5</span>)
</code></pre>
<h2 id="8-&#x5728;&#x6570;&#x7EC4;&#x4E2D;&#x8FDB;&#x884C;&#x8D4B;&#x503C;"><a name="8-&#x5728;&#x6570;&#x7EC4;&#x4E2D;&#x8FDB;&#x884C;&#x8D4B;&#x503C;" class="anchor-navigation-ex-anchor" href="#8-&#x5728;&#x6570;&#x7EC4;&#x4E2D;&#x8FDB;&#x884C;&#x8D4B;&#x503C;"><i class="fa fa-link" aria-hidden="true"></i></a>1.9. 8 &#x5728;&#x6570;&#x7EC4;&#x4E2D;&#x8FDB;&#x884C;&#x8D4B;&#x503C;</h2>
<p>&#x8FD9;&#x662F;&#x4E00;&#x7EBF;&#x6280;&#x5DE7;&#x3002;&#x53EA;&#x9700;&#x8981;&#x4E00;&#x884C;&#x4EE3;&#x7801;&#x5C31;&#x53EF;&#x4EE5;&#x8FDB;&#x884C;&#x4EA4;&#x6362;&#xFF0C;&#x66F4;&#x91CD;&#x8981;&#x7684;&#x662F;&#xFF0C;&#x65E0;&#x9700;&#x6570;&#x5B66;&#x8FD0;&#x7B97;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x6570;&#x7EC4;&#x7684;&#x57FA;&#x672C;&#x77E5;&#x8BC6;&#x3002;&#x4E0D;&#x8FC7;&#x5B83;&#x770B;&#x4E0A;&#x53BB;&#x53EF;&#x80FD;&#x5F88;&#x5947;&#x602A;&#x3002;</p>
<p>&#x5148;&#x8BA9;&#x770B;&#x770B;&#x5B83;&#x7684;&#x5B9E;&#x9645;&#x6548;&#x679C;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithArray</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num2 = [num1, num1 = num2][<span class="hljs-number">0</span>];

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

swapWithArray(<span class="hljs-number">2.3</span>,<span class="hljs-literal">Infinity</span>)
</code></pre>
<p>&#x5728;&#x6570;&#x7EC4;&#x7684;&#x4E0B;&#x6807; 0 &#x4F4D;&#x7F6E;&#x4E2D;&#x5B58;&#x50A8; <code>num1</code>&#xFF0C;&#x5728;&#x4E0B;&#x6807; 1 &#x4E2D;&#xFF0C;&#x65E2;&#x5C06; <code>num2</code> &#x5206;&#x914D;&#x7ED9; <code>num1</code>&#xFF0C;&#x53C8;&#x5B58;&#x50A8;&#x4E86; <code>num2</code>&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x662F;&#x8BBF;&#x95EE; <code>[0]</code>&#xFF0C;&#x5C06;&#x6570;&#x7EC4;&#x4E2D;&#x7684; <code>num1</code> &#x503C;&#x5B58;&#x50A8;&#x5728; <code>num2</code> &#x4E2D;&#x3002;&#x800C;&#x4E14;&#x53EF;&#x4EE5;&#x5728;&#x8FD9;&#x91CC;&#x4EA4;&#x6362;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x7684;&#x4EFB;&#x4F55;&#x4E1C;&#x897F;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;&#x6574;&#x6570;&#x3001;&#x6D6E;&#x70B9;&#x6570;&#xFF08;&#x5305;&#x62EC;&#x65E0;&#x7A77;&#x6570;&#xFF09;&#x4EE5;&#x53CA;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x770B;&#x4E0A;&#x53BB;&#x5F88;&#x6574;&#x6D01;&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x8FD9;&#x91CC;&#x5931;&#x53BB;&#x4E86;&#x4EE3;&#x7801;&#x7684;&#x6E05;&#x6670;&#x5EA6;&#x3002;</p>
<h2 id="9-&#x4F7F;&#x7528;&#x89E3;&#x6784;&#x8868;&#x8FBE;&#x5F0F;"><a name="9-&#x4F7F;&#x7528;&#x89E3;&#x6784;&#x8868;&#x8FBE;&#x5F0F;" class="anchor-navigation-ex-anchor" href="#9-&#x4F7F;&#x7528;&#x89E3;&#x6784;&#x8868;&#x8FBE;&#x5F0F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.10. 9 &#x4F7F;&#x7528;&#x89E3;&#x6784;&#x8868;&#x8FBE;&#x5F0F;</h2>
<p>&#x8FD9;&#x662F; ES6 &#x7684;&#x529F;&#x80FD;&#x3002;&#x8FD9;&#x662F;&#x6240;&#x6709;&#x65B9;&#x6CD5;&#x4E2D;&#x6700;&#x7B80;&#x5355;&#x7684;&#x3002;&#x53EA;&#x9700;&#x8981;&#x4E00;&#x884C;&#x4EE3;&#x7801;&#x5C31;&#x53EF;&#x4EE5;&#x5B8C;&#x6210;&#x4EA4;&#x6362;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> num1 = <span class="hljs-number">23.45</span>;
<span class="hljs-keyword">let</span> num2 = <span class="hljs-number">45.67</span>;

<span class="hljs-built_in">console</span>.log(num1,num2);

[num1,num2] = [num2,num1];

<span class="hljs-built_in">console</span>.log(num1,num2);
</code></pre>
<h2 id="10&#x3001;&#x4F7F;&#x7528;&#x7ACB;&#x5373;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;iife&#xFF09;"><a name="10&#x3001;&#x4F7F;&#x7528;&#x7ACB;&#x5373;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;iife&#xFF09;" class="anchor-navigation-ex-anchor" href="#10&#x3001;&#x4F7F;&#x7528;&#x7ACB;&#x5373;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;iife&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>1.11. 10&#x3001;&#x4F7F;&#x7528;&#x7ACB;&#x5373;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;IIFE&#xFF09;</h2>
<p>&#x8FD9;&#x662F;&#x6700;&#x5947;&#x602A;&#x7684;&#x4E00;&#x4E2A;&#x3002;&#x7B80;&#x5355;&#x7684;&#x8BF4; IIFE &#x662F;&#x5728;&#x5728;&#x5B9A;&#x4E49;&#x540E;&#x7ACB;&#x5373;&#x6267;&#x884C;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x53EF;&#x4EE5;&#x7528;&#x5B83;&#x6765;&#x4EA4;&#x6362;&#x4E24;&#x4E2A;&#x503C;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapWithIIFE</span>(<span class="hljs-params">num1,num2</span>)</span>{
  <span class="hljs-built_in">console</span>.log(num1,num2)

  num1 = (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">num2</span>)</span>{ <span class="hljs-keyword">return</span> num2; })(num2, num2=num1)

  <span class="hljs-built_in">console</span>.log(num1,num2)
}

swapWithIIFE(<span class="hljs-number">2.3</span>,<span class="hljs-number">3.4</span>)
</code></pre>
<p>&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x5728;&#x7B2C;4&#x884C;&#x7ACB;&#x5373;&#x8C03;&#x7528;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x3002;&#x6700;&#x540E;&#x7684;&#x62EC;&#x53F7;&#x662F;&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x3002;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x5C06; <code>num1</code> &#x8D4B;&#x503C;&#x7ED9; <code>num2</code>&#xFF0C;&#x4EC5;&#x4EC5;&#x8FD4;&#x56DE;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x4E0D;&#x8FC7;&#x8FD9;&#x79CD;&#x4EA4;&#x6362;&#x65B9;&#x6CD5;&#x6548;&#x7387;&#x4E0D;&#x9AD8;&#x3002; </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; zhengyuliang all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x7F16;&#x5199;&#x65F6;&#x95F4;&#xFF1A;
2020-09-10 17:23:56
</span></footer></body></html>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="前端异常的捕获与处理.html" class="navigation navigation-prev " aria-label="Previous page: 前端异常的捕获与处理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="常用的前端JavaScript方法封装.html" class="navigation navigation-next " aria-label="Next page: 常用的前端JavaScript方法封装">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"在 JavaScript 中交换值的 10 种方法","level":"4.15","depth":1,"next":{"title":"常用的前端JavaScript方法封装","level":"4.16","depth":1,"path":"javaScript/常用的前端JavaScript方法封装.md","ref":"javaScript/常用的前端JavaScript方法封装.md","articles":[]},"previous":{"title":"前端异常的捕获与处理","level":"4.14","depth":1,"path":"javaScript/前端异常的捕获与处理.md","ref":"javaScript/前端异常的捕获与处理.md","articles":[]},"dir":"ltr"},"config":{"plugins":["sectionx","splitter","tbfed-pagefooter","toggle-chapters","theme-comscore","-lunr","-search","search-plus","donate","anchor-navigation-ex","expandable-chapters","chapter-fold","expandable-chapters-small","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy zhengyuliang","modify_label":"该文件编写时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"pluginsConfig":{"anchor-navigation-ex":{"showLevel":false,"showGoTop":true,"isRewritePageTitle":true,"isShowTocTitleIcon":true,"tocLevel1Icon":"fa fa-hand-o-right","tocLevel2Icon":"fa fa-hand-o-right","tocLevel3Icon":"fa fa-hand-o-right"}},"livereload":{},"splitter":{},"donate":{"title":"","button":"赏","wechat":"https://gitee.com/htzyl/img_bed/raw/master/pay/weixin-min.jpg","alipay":"https://gitee.com/htzyl/img_bed/raw/master/pay/alipay-min.jpg","wechatText":"微信打赏","alipayText":"支付宝打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"sectionx":{"tag":"b"},"highlight":{},"anchor-navigation-ex":{"showLevel":true,"associatedWithSummary":true,"mode":"float","showGoTop":true,"printLog":false,"multipleH1":true,"float":{"floatIcon":"fa fa-navicon","showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""},"pageTop":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""}},"theme-comscore":{},"expandable-chapters-small":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"toggle-chapters":{},"expandable-chapters":{},"search-plus":{}},"theme":"default","author":"zhengyuliang","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"记录技术文档内容","language":"zh-hans","gitbook":">=3.0.0","description":"记录Gitbook的配置和一些插件的使用","theme-default":{"showLevel":true}},"file":{"path":"javaScript/在 JavaScript 中交换值的 10 种方法.md","mtime":"2020-09-10T09:23:56.515Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-01-06T09:44:01.178Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sectionx/sectionx.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

